import request from '@/utils/request'
import * as auth from '@/utils/auth'

// 登录action
export const login = (mobile, code) => {
  return async dispatch => {
    const data = await request.post('authorizations', { mobile, code })
    auth.setToken(data.token)
    await dispatch({ type: 'user/setToken', payload: data.token })
  }
}


// 退出action
export const logout = () => {
  auth.clearToken()
  return { type: 'user/setToken', payload: '' }
}

// 获取用信息
export const getUserInfo = () => {
  return async dispatch => {
    const data = await request.get('/user/profile')
    dispatch({ type: 'user/setName', payload: data.name })
  }
}